package com.timmy.google_play.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.timmy.google_play.utils.CheckUtils;
import com.timmy.google_play.utils.UIUtils;

/**
 * Created by Timmy on 2016/12/8.
 */

public abstract class BaseFragment extends Fragment {

    private LoadingPager mLoadingPager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mLoadingPager == null) {

            mLoadingPager = new LoadingPager(UIUtils.getContext()) {

                @Override
                public LoadedResult initData() {
                    return BaseFragment.this.initData();
                }

                @Override
                public View initSuccessView() {
                    return BaseFragment.this.initSuccessView();
                }
            };
        }

        //触发加载数据
        mLoadingPager.triggerLoadData();

        return mLoadingPager;
    }

    public LoadingPager getLoadingPager() {
        return mLoadingPager;
    }

    protected abstract LoadingPager.LoadedResult initData();

    protected abstract View initSuccessView();

    /**
     * 校验请求回来的数据
     */
    public LoadingPager.LoadedResult checkResData(Object object) {
        return CheckUtils.checkResData(object);
    }
}
